# _*_ coding utf-8 _*_
# george
# time: 2025/1/7上午9:34
# name: views.py
# comment: 路由+视图函数
import datetime

from flask import render_template, Blueprint, request, make_response, Response, redirect, url_for, session

# 创建蓝图
blue = Blueprint("user", __name__)


# @blue.route("/")
# def hello():
#     return "hello world"


@blue.route("/index")
def index():
    # 模板渲染
    return render_template("index.html", name="zs")
    # 返回字符串
    # return jsonify({"name": "zs"})


@blue.route("/index/getstr/<string:name>/")
def get_str(name):
    print(type(name))
    return name


@blue.route("/index/getint/<int:name>/")
def get_int(name):
    print(type(name))
    return "name" + str(name)


@blue.route("/index/getuuid/<uuid:uuid>")
def get_uuid(uuid):
    print(type(uuid))
    return str(uuid)


@blue.route("/index/putuuid/")
def put_uuid():
    import uuid
    return str(uuid.uuid4())


@blue.route("/index/any/<any(apple,banana,orange):fruit>/")
def get_any(fruit):
    print(type(fruit))
    return str(fruit)


@blue.route("/index/methods", methods=["GET", "POST"])
def methods():
    # 模板渲染
    return render_template("index.html", name="zs")


@blue.route("/request/")
def get_request():
    print(request)  # <Request 'http://127.0.0.1:5000/request/' [GET]>
    print(request.method)  # GET
    # http: // 127.0.0.1: 5000 / request /?name = zs & age = 13 /
    # ImmutableMultiDict;类字典对象,区别是可以出现重复的key,重复的key可以使用getlist()取值
    print(request.args)  # ImmutableMultiDict([('name', 'zs'), ('age', '13/')])
    print(request.args.get("name"))  # zs

    # POST请求的参数
    print(request.form)  # post请求的参数存储在这里

    # cookies
    print(request.cookies)

    # 其余参数
    print(request.path)
    print(request.url)
    print(request.base_url)  # 去掉参数
    print(request.host_url)  # 去掉参数,去掉路径
    print(request.remote_addr)  # 客户端的请求地址
    print(request.headers)  # 请求头
    print(request.files)  # 文件内容
    print(request.user_agent)  # 用户代理

    """
    请求的地址: http://127.0.0.1:5000/request/?name=zs&age=13/
    输出:
    /request/
    http://127.0.0.1:5000/request/?name=zs&age=13%2F
    http://127.0.0.1:5000/request/
    http://127.0.0.1:5000/
    127.0.0.1
    """

    return "request ok"


@blue.route("/get/response/")
def get_response():
    html = render_template("index.html", name="zs")
    print(html, type(html))
    res = Response(html)
    return res


# 直接重定向到具体的网址
# return redirect("https://www.baidu.com")
# 跳转到自己的服务器网址
# return redirect("/get/response/")

@blue.route("/redirect/")
def make_redirect():
    pass
    # url_for():反向解析
    # url_for('蓝图名称.视图函数名')
    ret = url_for("user.get_response")
    print("ret:", ret)  # ret: /get/response/
    # url_for 传参
    ret2 = url_for('user.get_request', name="王五", age=66)
    return redirect(ret)


# 会话技术
@blue.route("/home/")
def home():
    # username = request.cookies.get("user")
    # 获取session
    username = session.get("user")
    return render_template("home.html",username=username)


# 登陆
@blue.route("/")
@blue.route("/login/", methods=["GET", "POST"])
def login():
    if request.method == "GET":
        return render_template("login.html")

    username = request.form.get("username")
    password = request.form.get("password")
    print(username, password)

    if username == "zs" and password == "123":
        response = redirect("/home/")
        # response.set_cookie("user", username)  # 默认浏览器关闭,则cookie失效
        # max_age:s
        # expires: 指定过期时间
        # response.set_cookie("user", username, max_age=3600 * 24 * 7)  # 过期时间是s,现在设置时间为7天
        # response.set_cookie('user', username, expires=datetime.datetime(2025, 12, 12))

        # 设置session
        session["user"] = username
        session.permanent = True

        return response

    return "hahha"

@blue.route("/logout/")
def logout():
    # 删除cookie
    response = redirect("/login/")
    # response.delete_cookie('user')
    session.pop("user")
    # session.clear() # 慎用,会删除服务器下面的所有session
    return response